package com.mes.inventory.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.domain.Employee;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IMaterialDao;
import com.mes.inventory.domain.InvInfo;
import com.mes.inventory.domain.Material;

@Repository
public class MaterialDao extends BasicDao<Material> implements IMaterialDao {

	public Material getMaterialByBarcode(String barCode) {
		Material material = null;
		try {

			Session session = this.getSessionFactory().getCurrentSession();
			Criteria crit = session.createCriteria(Material.class);
			crit.add(Restrictions.eq("barCode", barCode));
			crit.setMaxResults(1);

			material = (Material) crit.uniqueResult();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return material;
	}

	public Material getMaterialByPartUidBatchNumSn(String partUid, String batchNum, String sn) {
		Material material = null;
		try {

			Session session = this.getSessionFactory().getCurrentSession();
			Criteria crit = session.createCriteria(Material.class);
			crit.add(Restrictions.eq("partUid", partUid));
			crit.add(Restrictions.eq("batchNum", batchNum));
			crit.add(Restrictions.eq("sn", sn));
			crit.setMaxResults(1);

			material = (Material) crit.uniqueResult();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return material;
	}
	
	public List<Material> getMaterialsByPartUidBatchNum(String partUid, String batchNum) {
		List<Material> list = null;
		try {

			Session session = this.getSessionFactory().getCurrentSession();
			Criteria crit = session.createCriteria(Material.class);
			crit.add(Restrictions.eq("partUid", partUid));
			crit.add(Restrictions.eq("batchNum", batchNum));
			crit.addOrder(Property.forName("batchNum").asc());

			list = (List<Material>) crit.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	

	}
	
	@Override
	public void getMaterial(Page<Material> page,Map<String, Object> parameter) throws Exception {
		
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from "
				+ Material.class.getName() + " a where 1=1 ");

		String partDrawingId = (String) parameter.get("partDrawingId");
		if (StringUtils.isNotEmpty(partDrawingId)) {
			coreHql.append(" and a.partDrawingId like :partDrawingId ");
			args.put("partDrawingId", "%" + partDrawingId + "%");
		}
		
		String partName = (String) parameter.get("partName");
		if (StringUtils.isNotEmpty(partName)) {
			coreHql.append(" and a.partName like :partName ");
			args.put("partName", "%" + partName + "%");
		}
		
		String batchNum = (String) parameter.get("batchNum");
		if (StringUtils.isNotEmpty(batchNum)) {
			coreHql.append(" and a.batchNum like :batchNum ");
			args.put("batchNum", "%" + batchNum + "%");
		}
		
		String partType = (String) parameter.get("partType");
		if (StringUtils.isNotEmpty(partType)) {
			coreHql.append(" and a.partType like :partType ");
			args.put("partType", "%" + partType + "%");
		}
		
		String partDescription = (String) parameter.get("partDescription");
		if (StringUtils.isNotEmpty(partDescription)) {
			coreHql.append(" and a.partDescription like :partDescription ");
			args.put("partDescription", "%" + partDescription + "%");
		}
		
		String sn = (String) parameter.get("sn");
		if (StringUtils.isNotEmpty(sn)) {
			coreHql.append(" and a.sn like :sn ");
			args.put("sn", "%" + sn + "%");
		}
		
		String countHql = "select count(*) " + coreHql.toString();
//			String hql = coreHql.toString() + "order by a.dept.id,a.id";
		String hql = coreHql.toString();
		
		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while(iterator.hasNext()){
			key = (String)iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":"+key, ":"+newKey);
			countHql = countHql.replace(":"+key, ":"+newKey);
			
			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
				this.pagingQuery(page, hql, countHql, newArgs);
		}
	}

